package com.ssy.lingxi.order.serviceimpl.feign;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.model.dto.OrderQuotationDTO;
import com.ssy.lingxi.order.service.feign.ITradeFeignService;
import com.ssy.lingxi.transaction.api.dto.response.InquiryListProductResponse;
import com.ssy.lingxi.transaction.api.feign.ITradeFeign;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 交易服务Feign接口调用实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-08-26
 */
@Service
public class TradeFeignServiceImpl implements ITradeFeignService {
    @Resource
    private ITradeFeign productQuotationControllerFeign;

    /**
     * 根据报价单号，查询订单商品信息
     *
     * @param quoteNo 报价单号
     * @return 查询结果
     */
    @Override
    public Wrapper<List<OrderQuotationDTO>> findInquiryOrderPrices(String quoteNo) {
        Wrapper<List<InquiryListProductResponse>> quoteResult = productQuotationControllerFeign.getInquiryProduct(quoteNo);
        if(quoteResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(quoteResult.getCode(), quoteResult.getMessage());
        }

        return Wrapper.success(CollectionUtils.isEmpty(quoteResult.getData()) ? new ArrayList<>() : quoteResult.getData().stream().map(p -> {
            OrderQuotationDTO quotation = new OrderQuotationDTO();
            quotation.setInquiryId(p.getInquiryListId());
            quotation.setQuoteId(p.getProductQuotationId());
            quotation.setProductId(p.getCommodityId());
            quotation.setSkuId(p.getProductId());
            quotation.setProductName(p.getProductName());
            quotation.setCategory(p.getCategory());
            quotation.setBrand(p.getBrand());
            quotation.setUnit(p.getUnit());
            quotation.setQuantity(BigDecimal.valueOf(p.getPurchaseCount()));
            quotation.setProductAmount(BigDecimal.valueOf(p.getMoney()));
            quotation.setPrice(BigDecimal.valueOf(p.getPrice()));
            quotation.setTaxRate(BigDecimal.valueOf(p.getTaxRate()));
            quotation.setMinimumQuantity(BigDecimal.valueOf(p.getMinOrder()));
            return quotation;
        }).collect(Collectors.toList()));
    }
}
